package com.nowcoder.community.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.nowcoder.community.controller.exception.PageNotFoundException;
import com.nowcoder.community.dao.EntityLikeDao;
import com.nowcoder.community.entity.DiscussPost;
import com.nowcoder.community.entity.Page;
import com.nowcoder.community.entity.User;
import com.nowcoder.community.mapper.DiscussPostMapper;
import com.nowcoder.community.mapper.UserMapper;
import com.nowcoder.community.util.Constants;
import com.nowcoder.community.util.HostHolder;
import com.nowcoder.community.util.JsonBody;
import com.nowcoder.community.util.SensitiveWordFilter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

@Service
public class DiscussPostService {
    @Autowired
    DiscussPostMapper discussPostMapper;

    @Autowired
    UserMapper userMapper;

    @Autowired
    EntityLikeDao entityLikeDao;

    @Autowired
    HostHolder hostHolder;

    public DiscussPost findPostById(int id) {
        return discussPostMapper.selectById(id);
    }

    /**
     * 增加指定id的贴子评论数
     */
    public void addCommentCountById(int id, int count) {
        discussPostMapper.addCommentCountById(id, count);
    }

    public List<DiscussPost> findDiscussPosts(int userId, int offset, int limit) {
        return discussPostMapper.findPageByUserId(userId, offset, limit);
    }

    public long countDiscussPosts() {
        return discussPostMapper.selectCount(new QueryWrapper<DiscussPost>(){
            {ne("status", 2);}
        });
    }

    public JsonBody addPost(DiscussPost post) {
        if (SensitiveWordFilter.hasSensitive(post.getTitle())) {
            return JsonBody.error("标题含敏感词, 发布失败.");
        }

        post.setStatus(0);
        post.setType(0);
        post.setContent(SensitiveWordFilter.filter(post.getContent()));
        discussPostMapper.insert(post);
        return JsonBody.ok("发布成功!");
    }

    public ModelAndView getPostDetailPage(int postId, Page commentPage) throws PageNotFoundException {
        ModelAndView ret = new ModelAndView("/site/discuss-detail");
        DiscussPost post = discussPostMapper.selectById(postId);
        if (post == null) {
            throw new PageNotFoundException("没有找到该帖子.");
        }
        // post其他信息处理
        post.setLikeCount(entityLikeDao.selectEntityLikeCount(Constants.ENTITY_TYPE_POST, postId));
        if (hostHolder.getUser() != null) {
            post.setLikeStatus(entityLikeDao.getLikeStatus(hostHolder.getUser().getId(), Constants.ENTITY_TYPE_POST, postId));
        }

        // 评论处理
        commentPage.setPath("/discuss/detail/" + postId);
        commentPage.setRows(post.getCommentCount() == null ? 0 : post.getCommentCount());

        // model构造
        User author = userMapper.selectById(post.getUserId());
        ret.addObject("post", post);
        ret.addObject("author", author);
        ret.addObject("page", commentPage);

        return ret;
    }
}
